//
//  FriendImpressionVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/28.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "FriendImpressionVC.h"
#import "FriendImpresstionSecCell.h"
#import "FriendImpressionViewController.h"
@interface FriendImpressionVC ()
<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * _dataSource;
    
    NSMutableArray * tempArr;
    
    NSString * tempStr;
}
@end

@implementation FriendImpressionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    [self addRightItem];
self.navigationItem.title = @"好友印象";
   // [self addBackItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    _dataSource = [[NSMutableArray alloc]init];
    tempArr = [[NSMutableArray alloc]init];
//    [_dataSource addObjectsFromArray:@[@"有为青年",@"幽默",@"完美主义",@"好看",@"个人主义",@"天秤座",@"电影达人",@"k歌之王",@"双鱼座",@"乐观天真",@"二逼青年",@"善良",@"自由主义"]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"FriendImpressionFirCell" bundle:nil] forCellReuseIdentifier:@"FriendImpressionFirCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"FriendImpresstionSecCell" bundle:nil] forCellReuseIdentifier:@"FriendImpresstionSecCell"];
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestData];
}
-(void)requestData
{
    [U76HONet recommendLableWitnAction:@"" andDataBlock:^(RecommenLableModel *model) {
        NSLog(@"推荐标签获取数据成功了");
        [_dataSource addObjectsFromArray:model.labArray];
        [_tableView reloadData];
    }];
}
-(void)submitLabelWithStr:(NSString *)str
{
    __weak typeof(self)weakSelf = self;
    [U76HONet giveFriendLableWtionAction:@"" code1:_friendCode code:[Singleton share].code tags:str andDataBlock:^(NSDictionary *dic) {
        NSLog(@"给好友打标签成功上传到服务器");
        _friendImpressBlock();
        [Singleton alerSendMessage:@"评价成功"];
        [weakSelf performSelector:@selector(backItem:) withObject:nil afterDelay:1.0];
    }];
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}

#pragma mark - _tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        FriendImpresstionSecCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FriendImpresstionSecCell"];
        cell.dataArr = _dataSource ;
    __weak typeof(self)weakSelf = self;
        [cell setBlock:^(int tag) {
            NSLog(@"tag:%i",tag);
            tempStr = _dataSource[tag-1];
            [weakSelf addtextContent];
        }];
        cell.backgroundColor = UIColorFromRGB(0xf1f3f2);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self createTableViewHeader];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat totalW1 = 0;
    CGFloat width1 = SCREEN_WIDTH-SIZETO6W(30)*2;
    int j1 = 0;
    for (int i = 0; i<_dataSource.count; i++) {
        NSString * str = _dataSource[i];
        CGFloat w = [str widthOfSize:18];
        totalW1 += w+SIZETO6W(10);
        if (totalW1<=width1) {

        }
        else
        {
            totalW1 = w+SIZETO6W(15);
            j1 = j1+1;

        }
    }
    CGFloat h1 = SIZETO6H(15)*2 + 30 + j1*(30+SIZETO6H(10));

    return h1+SIZETO6H(15+40)+20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat totalW1;
    totalW1 = 0;
    CGFloat width1 = SCREEN_WIDTH-SIZETO6W(15)*2;
    int j1 = 0;
    for (int i = 0; i<_dataSource.count; i++) {
        NSString * str = _dataSource[i];
        CGFloat w = [str widthOfSize:18];
        totalW1 += w+SIZETO6W(10);
        if (totalW1<=width1) {
        }
        else
        {
            totalW1 = w+SIZETO6W(15);
            j1 = j1+1;
        }
        
    }
    CGFloat h1 = 100+SIZETO6H(15)*2 + 30 + j1*(30+SIZETO6H(10));
    return h1;
}
#pragma mark - createTableViewHeader
-(UIView *)createTableViewHeader
{
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.tag = 504;
    view.userInteractionEnabled = YES;
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6W(15), SCREEN_WIDTH-SIZETO6W(15)*2, 150)];
    subView.backgroundColor = [UIColor whiteColor];
    subView.tag = 505;
    
    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, subView.width, subView.height)];
    textView.tag = 712;
    [subView addSubview:textView];
    
    [view addSubview:subView];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString * str = @"看看其他人对TA的印象";
    btn.frame = CGRectMake(SCREEN_WIDTH-[str widthOfSize:18], subView.bottom+SIZETO6W(15), [str widthOfSize:18], 30);
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitle:str forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(otherPeopleAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateNormal];
    [view addSubview:btn];
    
    
    return view;

}
-(void)addtextContent
{
    UITextView * textView = (UITextView *)[self.view viewWithTag:712];
    textView.text = tempStr;
}
-(void)otherPeopleAction:(UIButton *)btn
{
    
    FriendImpressionViewController * otherVC = [[FriendImpressionViewController alloc]init];
    otherVC.isNeedDescription = YES;
    otherVC.friendCode = _friendCode;
    [self.navigationController pushViewController:otherVC animated:YES];
}
#pragma mark - addRightItem
-(void)addRightItem
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 30);
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
#pragma mark --提交点击事件
-(void)submitAction:(UIButton *)btn
{
    UITextView * textView = (UITextView *)[self.view viewWithTag:712];

    if ([self isLogin]) {
        if (![textView.text isEqualToString:@""]) {
            [self submitLabelWithStr:textView.text];
        }
        else
        {
            [Singleton alerSendMessage:@"不能为空"];
        }
    }
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.editing = YES;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.editing = YES;
    UITextView * textView = (UITextView *)[self.view viewWithTag:712];
    [textView resignFirstResponder];
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
